package cn.tedu.mapper;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * Text:k2 单词
 * LongWritable: v2,出现一次
 * Text: k3 单词
 * LongWritable; v3,总共出现的次数
 */
public class MyReducer extends Reducer<Text, LongWritable,Text,LongWritable> {
    /**
     * reduce是数据的聚合,每一个单词所有出现次数聚合在一起
     *
     * @param key 单词
     * @param values 出现次数的聚合
     * @param context 上下文
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
        long total = 0;
        //通过遍历values累加求和,出现的次数的和
        for (LongWritable value : values) {
            total+=value.get();
        }
        //将结果输出
        context.write(key, new LongWritable(total));
    }
}
